package jaspect.model;

import jaspect.Main;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Abin
 */
public class MakeFile {

    private static List<String> srcFile;
    static StringBuilder input;
    static DefaultMutableTreeNode cn1;
    static DefaultMutableTreeNode cn2;
    static DefaultMutableTreeNode cn3;
    static DefaultMutableTreeNode cn4;

    /**
     *
     * @param path
     * @throws IOException
     */
    public static void newMake(String path) throws IOException {
        String fileName = path + "\\" + "project.pro";

        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
        out.write("Name = " + "new project" + "\n");
        out.write("SRCDIR = " + path + "\\" + "src\n");
        out.write("INCDIR = " + path + "\\" + "inc\n");
        out.write("LIBDIR = " + path + "\\" + "lib\n");
        out.write("BUILDDIR = " + path + "\\" + "build\n");
        out.close();





    }

    /**
     *
     * @param path
     * @throws IOException
     */
    public static void openMake(String path) throws IOException {
        FileInputStream fstream = null;

        path = path + "\\src";
        input = new StringBuilder();
        setSrcFile((List<String>) new ArrayList());
        String[] children = new File(path).list();
        if (children == null) {

            System.out.println("Either dir does not exist or is not a directory");
        } else {
            for (int i = 0; i < children.length; i++) { // Get filename of file or directory
                String fileName = children[i];
                getSrcFile().add(fileName);


            }
        }

        addNodes("src");
    }

    /**
     *
     */
    public static void readMake() {
        int start = 0;
        int end = 0;
        String fileName;
        setSrcFile((List<String>) new ArrayList());
        while (true) {
            start = input.indexOf("+", end);
            if (start == -1) {
                break;
            }
            end = input.indexOf("\n", start);
            if (end == -1) {
                break;
            }
            fileName = input.substring(start + 1, end);
            getSrcFile().add(fileName);
        }




    }

    /**
     * This method will add a project tree with the name 'name' and 4 folders namely
     * src,build,inc,lib
     *
     * @param path
     * @throws IOException
     */
    public static void newProject(String path) throws IOException {

        File f;

        f = new File(path + "\\src");
        f.mkdir();

        f = new File(path + "\\build");
        f.mkdir();

        f = new File(path + "\\inc");
        f.mkdir();

        f = new File(path + "\\lib");
        f.mkdir();




        String name = path.substring(path.lastIndexOf("\\") + 1, path.length());

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(name);



        cn1 = new DefaultMutableTreeNode("src");
        cn2 = new DefaultMutableTreeNode("build");
        cn3 = new DefaultMutableTreeNode("inc");
        cn4 = new DefaultMutableTreeNode("lib");
        root.add(cn1);
        root.add(cn2);
        root.add(cn3);
        root.add(cn4);
        Main.getTm().setRoot(root);





    }

    /**
     * adds a single file into to the file.
     * @param fileName
     * @throws IOException
     */
    public static void addFile(String fileName) throws IOException {
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName, false));
        out.write("\t+" + fileName + "\n");
        out.close();
        addNode(fileName);

    }

    /**
     *
     * @param fileName
     */
    public static void addNode(String fileName) {
        //nod for the "src"
        DefaultMutableTreeNode nod = null;
        nod = cn1;
        DefaultMutableTreeNode fl = new DefaultMutableTreeNode(fileName);

        Main.getTm().insertNodeInto(fl, nod, 0);

    }

    /**
     * You will get the list of files as argument add it to tree directory 'src'
     * @param nodeName
     */
    public static void addNodes(String nodeName) {

        DefaultMutableTreeNode nod = null;
        if (nodeName.equals("src")) {
            nod = cn1;
        }
        if (nodeName.equals("build")) {
            nod = cn2;
        }
        if (nodeName.equals("inc")) {
            nod = cn3;
        }
        if (nodeName.equals("lib")) {
            nod = cn4;
        }

        for (int i = 0; i < getSrcFile().size(); i++) {
            DefaultMutableTreeNode fl = new DefaultMutableTreeNode(getSrcFile().get(i));

            Main.getTm().insertNodeInto(fl, nod, i);
        }
    }

    /**
     * @return the srcFile
     */
    public static List<String> getSrcFile() {
        return srcFile;
    }

    /**
     * @param aSrcFile the srcFile to set
     */
    public static void setSrcFile(List<String> aSrcFile) {
        srcFile = aSrcFile;
    }
}
